package com.ouman.luoliluoli;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ouman.luoliluoli.articlefragments.ArticleFragmentPagerAdapter;
import com.ouman.luoliluoli.articlefragments.DailyFeedsFragment;
import com.ouman.luoliluoli.articlefragments.DuanziShouFragment;
import com.ouman.luoliluoli.articlefragments.NiceArticleFragment;
import com.ouman.luoliluoli.articlefragments.SingleWordFragment;
import com.ouman.luoliluoli.galleryfragments.GalleryFragmentPagerAdapter;
import com.ouman.luoliluoli.galleryfragments.HotGirlsFragment;
import com.ouman.luoliluoli.galleryfragments.MeiziFragment;


public class ArticleFragment extends Fragment {

    private OnFragmentInteractionListener mListener;
    private int themeColor;

    public ArticleFragment() {
        // Required empty public constructor
    }


    public static ArticleFragment newInstance(String param1, String param2) {
        ArticleFragment fragment = new ArticleFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        themeColor = typedValue.data;
        System.out.println(themeColor);
        System.out.println("[IMPORTANT] got activity primary color: " + themeColor);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        ((MainActivity) getActivity()).setAppBarElevation(0);
        ((MainActivity) getActivity()).disableCollapsingLayoutTitle();
        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("文章");

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager_tab_article);
        setupViewPager(viewPager);

        //find tab layout in article fragment
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs_article);
        tabLayout.setBackgroundColor(themeColor);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setElevation(6);

        return view;
    }

    private void setupViewPager(ViewPager viewPager) {
        ArticleFragmentPagerAdapter adapter = new ArticleFragmentPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new DailyFeedsFragment(), "新鲜事");
        adapter.addFragment(new DuanziShouFragment(), "段子");
        adapter.addFragment(new NiceArticleFragment(), "美文");
        adapter.addFragment(new SingleWordFragment(), "一句话电影");
        viewPager.setAdapter(adapter);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
